/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IFSv2.Operations;

import IFSv2.Util.utilCryptUtility;
import IFSv2.Connection.DBConnection;
import IFSv2.Util.utilSystemPath;
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Joel
 */
public class clsLogin {
        
    private String KEY_FILE;
    
    public boolean login(String username, String password) {    
        
        KEY_FILE = utilSystemPath.getKeyPath()+"tim.key";
        
        String _sql = "SELECT Password FROM userinformation WHERE UserName = '"+ username +"'";
        
        ResultSet rsLoginData;
        
        try {
            rsLoginData = DBConnection.getActiveConnection().createStatement().executeQuery(_sql);
            
            if(!rsLoginData.next()) {
                return false;
            }
            
            try {
                String pswrd = utilCryptUtility.decrypt(rsLoginData.getString("Password"),new File(KEY_FILE));
                
                if(password.equals(pswrd)) {
                    return true;
                }
            } catch (GeneralSecurityException | IOException ex) {
                Logger.getLogger(clsConnectionProvider.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }

        } catch (SQLException ex) {
            Logger.getLogger(clsConnectionProvider.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }

        return false;
    }    
    
    
}
